<template>
  <div class="huangli-display">
    <a-card class="huangli-card" :bordered="false">
      <template #title>
        <div class="title">
          <span>📅 今日黄历 🧿</span>
        </div>
      </template>

      <div v-if="loading" class="loading-container">
        <a-spin tip="正在获取黄历数据...">
          <div class="loading-content">
            <a-skeleton active :paragraph="{ rows: 6 }" />
          </div>
        </a-spin>
      </div>

      <div v-else-if="huangliData" class="huangli-content">
        <!-- 日期信息卡片 -->
        <div class="date-card">
          <div class="date-content">
            <div class="solar-date">
              <calendar-outlined />
              <span>{{ huangliData.时间 }}</span>
            </div>

            <div class="lunar-info">
              <div class="lunar-date">
                <span class="emoji-icon">🌙</span>
                <span>{{ huangliData.农历 }}</span>
              </div>
              <div class="ganzhi">
                <history-outlined />
                <span>{{ huangliData.天干地支 }}</span>
              </div>
            </div>

            <div class="solar-term-time">
              <div class="solar-term" v-if="huangliData.节气">
                <environment-outlined />
                <span>节气：{{ huangliData.节气 }}</span>
              </div>
              <div class="current-time">
                <clock-circle-outlined />
                <span>时辰：{{ huangliData.时辰 }}</span>
              </div>
            </div>
          </div>
        </div>

        <!-- 星座运势卡片 -->
        <div class="fortune-card">
          <div class="card-header">
            <star-outlined />
            <span>今日运势</span>
          </div>

          <div class="quote-box">
            <div class="quote-icon">💭</div>
            <div class="quote-content">{{ huangliData.星座运势.今日一言 }}</div>
          </div>

          <div class="fortune-grid">
            <div class="fortune-item">
              <div class="fortune-icon">⭐</div>
              <div class="fortune-label">综合运势</div>
              <div class="fortune-value">
                <a-rate :value="huangliData.星座运势.综合运势" disabled allow-half />
              </div>
            </div>

            <div class="fortune-item">
              <div class="fortune-icon">🎨</div>
              <div class="fortune-label">幸运颜色</div>
              <div class="fortune-value">
                <div class="color-dot" :style="{ backgroundColor: getLuckyColor(huangliData.星座运势.幸运颜色) }"></div>
                {{ huangliData.星座运势.幸运颜色 }}
              </div>
            </div>

            <div class="fortune-item">
              <div class="fortune-icon">🔢</div>
              <div class="fortune-label">幸运数字</div>
              <div class="fortune-value">{{ huangliData.星座运势.幸运数字 }}</div>
            </div>

            <div class="fortune-item">
              <div class="fortune-icon">💞</div>
              <div class="fortune-label">速配星座</div>
              <div class="fortune-value">{{ huangliData.星座运势.速配星座 }}</div>
            </div>
          </div>
        </div>

        <!-- 黄历详解卡片 -->
        <div class="explanation-card">
          <div class="card-header">
            <read-outlined />
            <span>黄历详解</span>
          </div>

          <div class="explanation-content">
            <a-collapse ghost>
              <a-collapse-panel key="1" header="天干地支解析">
                <div class="explanation-text">
                  <p><strong>{{ huangliData.天干地支 }}</strong> 中：</p>
                  <p>• <strong>乙巳年</strong>：乙属木，巳属火。木生火，年柱相生，有利于事业发展。</p>
                  <p>• <strong>庚辰月</strong>：庚属金，辰属土。土生金，月柱相生，财运较好。</p>
                  <p>• <strong>丁未日</strong>：丁属火，未属土。火生土，日柱相生，人际关系和谐。</p>
                </div>
              </a-collapse-panel>

              <a-collapse-panel key="2" header="时辰吉凶">
                <div class="explanation-text">
                  <p><strong>{{ huangliData.时辰 }}</strong>：酉时为傍晚17:00-19:00。</p>
                  <p>酉时五行属金，与日主五行丁火相克，宜静不宜动，适合总结反思、收尾工作。</p>
                </div>
              </a-collapse-panel>

              <a-collapse-panel key="3" header="节气说明">
                <div class="explanation-text">
                  <p><strong>{{ huangliData.节气 }}</strong>：清明是二十四节气之一，清明后天气渐暖。</p>
                  <p>清明后，万物生长，适合户外活动和播种栽培。</p>
                </div>
              </a-collapse-panel>
            </a-collapse>
          </div>
        </div>

        <!-- 今日建议卡片 -->
        <div class="suggestion-card">
          <div class="card-header">
            <bulb-outlined />
            <span>今日建议</span>
          </div>

          <div class="suggestion-content">
            <div class="suggestion-item">
              <check-circle-outlined style="color: #52c41a" />
              <span>宜：思考、计划、总结、学习、冥想</span>
            </div>
            <div class="suggestion-item">
              <close-circle-outlined style="color: #f5222d" />
              <span>忌：冲动决策、过度消费、熬夜、暴饮暴食</span>
            </div>
            <div class="suggestion-item">
              <info-circle-outlined style="color: #1890ff" />
              <span>建议：今天适合内省和规划，保持平和心态，避免情绪波动。</span>
            </div>
          </div>
        </div>

        <div class="refresh-section">
          <a-button type="primary" @click="refreshData" :loading="loading">
            <template #icon><reload-outlined /></template>
            刷新数据
          </a-button>
          <span class="last-update">最后更新: {{ lastUpdateTime }}</span>
        </div>
      </div>

      <div v-else class="empty-state">
        <a-empty description="暂无黄历数据">
          <template #image>
            <img src="https://gw.alipayobjects.com/zos/antfincdn/ZHrcdLPrvN/empty.svg" alt="empty" />
          </template>
          <template #description>
            <span>暂无黄历数据，请点击下方按钮获取</span>
          </template>
          <a-button type="primary" @click="fetchHuangliData">
            获取今日黄历
          </a-button>
        </a-empty>
      </div>
    </a-card>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import { message } from 'ant-design-vue';
import axios from 'axios';
import {
  CalendarOutlined,
  ClockCircleOutlined,
  StarOutlined,
  EnvironmentOutlined,
  HistoryOutlined,
  ReadOutlined,
  BulbOutlined,
  CheckCircleOutlined,
  CloseCircleOutlined,
  InfoCircleOutlined,
  ReloadOutlined
} from '@ant-design/icons-vue';

// 状态定义
const huangliData = ref(null);
const loading = ref(false);
const lastUpdateTime = ref('');

// 模拟数据（用于开发和测试）
const mockData = {
  "code": 200,
  "data": {
    "时间": "2025年04月08日",
    "节气": "清明后",
    "时辰": "酉时",
    "星座运势": {
      "今日一言": "你要做的不是合群，而是从人群中脱颖而出。",
      "综合运势": 3,
      "幸运颜色": "深绿",
      "幸运数字": 28,
      "速配星座": "双鱼座"
    },
    "农历": "农历 二零二五 三月 十一日",
    "天干地支": "乙巳年庚辰月丁未日"
  }
};

// 获取黄历数据
const fetchHuangliData = async () => {
  loading.value = true;

  try {
    // 实际项目中使用下面的API调用
    const response = await axios.get('https://api.qster.top/API/v1/huanli/');

    // 使用模拟数据进行测试（实际项目中请注释掉这行）
    // const response = { data: mockData };

    if (response.data.code === 200) {
      huangliData.value = response.data.data;
      lastUpdateTime.value = new Date().toLocaleString('zh-CN');
      message.success('黄历数据获取成功');
    } else {
      message.error(response.data.msg || '获取黄历数据失败');
    }
  } catch (error) {
    console.error('获取黄历数据失败:', error);
    message.error('获取黄历数据失败，请稍后重试');
  } finally {
    loading.value = false;
  }
};

// 刷新数据
const refreshData = () => {
  fetchHuangliData();
};

// 获取幸运颜色的CSS颜色值
const getLuckyColor = (colorName) => {
  const colorMap = {
    '红': '#f5222d',
    '橙': '#fa8c16',
    '黄': '#fadb14',
    '绿': '#52c41a',
    '深绿': '#237804',
    '青': '#13c2c2',
    '蓝': '#1890ff',
    '深蓝': '#003a8c',
    '紫': '#722ed1',
    '粉': '#eb2f96',
    '棕': '#a0522d',
    '灰': '#8c8c8c',
    '白': '#ffffff',
    '黑': '#000000'
  };

  // 模糊匹配颜色名称
  for (const key in colorMap) {
    if (colorName.includes(key)) {
      return colorMap[key];
    }
  }

  return '#1890ff'; // 默认颜色
};

// 页面加载时获取数据
onMounted(() => {
  fetchHuangliData();
});
</script>

<style scoped>
.huangli-display {
  max-width: 800px;
  margin: 0 auto;
  padding: 20px;
}

.huangli-card {
  border-radius: 16px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.1);
  overflow: hidden;
  background: linear-gradient(to bottom, #ffffff, #fffbf0);
}

.title {
  font-size: 1.8rem;
  text-align: center;
  background: linear-gradient(45deg, #ff9500, #ff3852);
  -webkit-background-clip: text;
  background-clip: text;

  font-weight: bold;
  padding: 8px 0;
}

.loading-container, .empty-state {
  padding: 60px 0;
  text-align: center;
}

.loading-content {
  padding: 20px;
  background-color: rgba(255, 255, 255, 0.8);
  border-radius: 12px;
}

.huangli-content {
  animation: fadeIn 0.5s ease-in-out;
  display: flex;
  flex-direction: column;
  gap: 20px;
}

/* 日期卡片样式 */
.date-card {
  background-color: #fff;
  border-radius: 12px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
  overflow: hidden;
  padding: 20px;
  border-left: 5px solid #ff9500;
}

.date-content {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.solar-date {
  font-size: 1.8rem;
  font-weight: bold;
  color: #333;
  display: flex;
  align-items: center;
  gap: 10px;
}

.lunar-info {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.lunar-date, .ganzhi {
  font-size: 1rem;
  color: #666;
  display: flex;
  align-items: center;
  gap: 10px;
}

.emoji-icon {
  font-size: 1.2rem;
  line-height: 1;
}

.solar-term-time {
  display: flex;
  flex-wrap: wrap;
  gap: 16px;
  margin-top: 8px;
}

.solar-term, .current-time {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 12px;
  background-color: #f9f9f9;
  border-radius: 20px;
  font-size: 0.9rem;
}

/* 星座运势卡片样式 */
.fortune-card {
  background-color: #fff;
  border-radius: 12px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
  overflow: hidden;
  padding: 20px;
  border-left: 5px solid #722ed1;
}

.card-header {
  font-size: 1.2rem;
  font-weight: bold;
  margin-bottom: 16px;
  display: flex;
  align-items: center;
  gap: 8px;
  color: #333;
}

.quote-box {
  background-color: #f0f5ff;
  border-radius: 12px;
  padding: 16px;
  margin-bottom: 20px;
  display: flex;
  align-items: flex-start;
  gap: 12px;
}

.quote-icon {
  font-size: 1.5rem;
  line-height: 1;
}

.quote-content {
  font-size: 1.1rem;
  color: #333;
  font-style: italic;
  line-height: 1.5;
}

.fortune-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
  gap: 16px;
}

.fortune-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  padding: 16px;
  background-color: #f9f9f9;
  border-radius: 8px;
  transition: all 0.3s;
}

.fortune-item:hover {
  transform: translateY(-3px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.fortune-icon {
  font-size: 1.8rem;
  margin-bottom: 8px;
}

.fortune-label {
  font-weight: bold;
  margin-bottom: 8px;
}

.fortune-value {
  font-size: 1rem;
  color: #666;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}

.color-dot {
  width: 16px;
  height: 16px;
  border-radius: 50%;
  border: 1px solid #d9d9d9;
}

/* 黄历详解卡片样式 */
.explanation-card {
  background-color: #fff;
  border-radius: 12px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
  overflow: hidden;
  padding: 20px;
  border-left: 5px solid #1890ff;
}

.explanation-text {
  font-size: 0.95rem;
  line-height: 1.6;
  color: #333;
}

.explanation-text p {
  margin-bottom: 8px;
}

/* 今日建议卡片样式 */
.suggestion-card {
  background-color: #fff;
  border-radius: 12px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.05);
  overflow: hidden;
  padding: 20px;
  border-left: 5px solid #52c41a;
}

.suggestion-content {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.suggestion-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 16px;
  background-color: #f9f9f9;
  border-radius: 8px;
  font-size: 1rem;
}

.refresh-section {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 16px;
  margin-top: 16px;
}

.last-update {
  font-size: 0.9rem;
  color: #666;
}

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(10px); }
  to { opacity: 1; transform: translateY(0); }
}

@media (max-width: 768px) {
  .huangli-display {
    padding: 10px;
  }

  .title {
    font-size: 1.5rem;
  }

  .solar-date {
    font-size: 1.5rem;
  }

  .fortune-grid {
    grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  }

  .solar-term-time {
    flex-direction: column;
    gap: 8px;
  }

  .refresh-section {
    flex-direction: column;
    gap: 8px;
  }
}
</style>
